﻿using System.Data;
using System.Linq;
using viBook.Models;

namespace viBook.Repositories
{
    public class ReportRepository : IReportRepository
    {
        private readonly viBookContext _context;
        private readonly IUserRepository _userRepository;
        private readonly IChapterCommentRepository _chapterCommentRepository;
        private readonly IBookCommentRepository _bookCommentRepository;
        private readonly IBookRepository _bookRepository;
        private readonly IChapterRepository _chapterRepository;

        public ReportRepository(IUserRepository userRepository, IChapterCommentRepository chapterCommentRepository, IBookCommentRepository bookCommentRepository, IBookRepository bookRepository, IChapterRepository chapterRepository)
        {
            _context = new viBookContext();
            _userRepository = userRepository;
            _chapterCommentRepository = chapterCommentRepository;
            _bookCommentRepository = bookCommentRepository;
            _bookRepository = bookRepository;
            _chapterRepository = chapterRepository;
        }

        #region IReportRepository Members

        public IQueryable<ReportOnChapterComment> AllReportOnChapterComment
        {
            get { return _context.ReportOnChapterComments; }
        }

        public IQueryable<ReportOnBookComment> AllReportOnBookComment
        {
            get { return _context.ReportOnBookComments; }
        }

        public IQueryable<ReportOnBook> AllReportOnBook
        {
            get { return _context.ReportOnBooks; }
        }

        public IQueryable<ReportOnUser> AllReportOnUser
        {
            get { return _context.ReportOnUsers; }
        }

        public void ApproveReportOnChapterComment(int id)
        {
            var report = _context.ReportOnChapterComments.Find(id);
            if (report != null)
            {
                var comment = _context.ChapterComments.AsNoTracking().Where(c => c.Id == report.ChapterCommentId).FirstOrDefault();
                comment.IsBlocked = true;
                _chapterCommentRepository.InsertOrUpdate(comment);
                _context.ReportOnChapterComments.Remove(report);
                Save();
            }
        }

        public void ApproveReportOnBookComment(int id)
        {
            var report = _context.ReportOnBookComments.Find(id);
            if (report != null)
            {
                var comment = _context.BookComments.Find(report.BookCommentId);
                comment.IsBlocked = true;
                _bookCommentRepository.InsertOrUpdate(comment);
                _context.ReportOnBookComments.Remove(report);
                Save();
            }
        }

        public void ApproveReportOnBook(int id)
        {
            var report = _context.ReportOnBooks.Find(id);
            if (report != null)
            {
                var book = _context.Books.Find(report.BookId);
                book.IsBlocked = true;
                _bookRepository.InsertOrUpdate(book);
                _context.ReportOnBooks.Remove(report);
                Save();
            }
        }

        public void ApproveReportOnUser(int id)
        {
            var report = _context.ReportOnUsers.Find(id);
            if (report != null)
            {
                var user = _context.Users.Find(report.ReporteeId);
                user.IsBlocked = true;
                _userRepository.InsertOrUpdate(user);
                _context.ReportOnUsers.Remove(report);
                Save();
            }
        }

        public void DisapproveReportOnChapterComment(int id)
        {
            var report = _context.ReportOnChapterComments.Find(id);
            if (report != null)
            {
                var chapterComment = _context.ChapterComments.AsNoTracking().Where(c => c.Id == report.ChapterCommentId).FirstOrDefault();
                chapterComment.IsBlocked = false;
                _chapterCommentRepository.InsertOrUpdate(chapterComment);
                _context.ReportOnChapterComments.Remove(report);
                Save();
            }
        }

        public void DisapproveReportOnBookComment(int id)
        {
            var report = _context.ReportOnBookComments.Find(id);
            if (report != null)
            {
                var bookComment = _context.BookComments.Find(report.BookCommentId);
                bookComment.IsBlocked = false;
                _bookCommentRepository.InsertOrUpdate(bookComment);
                _context.ReportOnBookComments.Remove(report);
                Save();
            }
        }

        public void DisapproveReportOnBook(int id)
        {
            var report = _context.ReportOnBooks.Find(id);
            if (report != null)
            {
                var book = _context.Books.Find(report.BookId);
                book.IsBlocked = false;
                _bookRepository.InsertOrUpdate(book);
                _context.ReportOnBooks.Remove(report);
                Save();
            }
        }

        public void DisapproveReportOnUser(int id)
        {
            var report = _context.ReportOnUsers.Find(id);
            if (report != null)
            {
                var user = _context.Users.Find(report.UserId);
                user.IsBlocked = false;
                _userRepository.InsertOrUpdate(user);
                _context.ReportOnUsers.Remove(report);
                Save();
            }
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        #endregion


    }

    public interface IReportRepository
    {
        IQueryable<ReportOnChapterComment> AllReportOnChapterComment { get; }
        IQueryable<ReportOnBookComment> AllReportOnBookComment { get; }
        IQueryable<ReportOnBook> AllReportOnBook { get; }
        IQueryable<ReportOnUser> AllReportOnUser { get; }

        void ApproveReportOnChapterComment(int id);
        void ApproveReportOnBookComment(int id);
        void ApproveReportOnBook(int id);
        void ApproveReportOnUser(int id);

        void DisapproveReportOnChapterComment(int id);
        void DisapproveReportOnBookComment(int id);
        void DisapproveReportOnBook(int id);
        void DisapproveReportOnUser(int id);
        void Save();
    }

}